import * as api from '@/api/article.js'

export default {
    namespaced: true,
    state: {
        channels: [],
        articles: [],
        allchannels: [],
        news: []
    },
    mutations: {
        SET_CHANNELS(state, newList) {
            state.channels = newList
            console.log(state.channels);
        },
        SET_ALLCHANNELS(state, newList) {
            state.allchannels = newList
            console.log(state.allchannels);
        },
        SET_ARTICLES(state, newList) {
            state.articles = newList
            console.log(state.articles);
        },
        SET_NEWS(state, newList) {
            state.news = newList
            // console.log(state.news);
        },
    },
    actions: {
        async updateChannels(context) {
            const res = await api.getUserChannels()
            context.commit('SET_CHANNELS', res.data.data.channels)
        },
        async updateAllChannels(context) {
            const res = await api.getAllChannels()
            context.commit('SET_ALLCHANNELS', res.data.data.channels)
        },
        async updateArticles(context, params) {
            context.commit('SET_ARTICLES', [])
            const res = await api.getArticles(params)
            context.commit('SET_ARTICLES', res.data.data.results)
        },
        /**
         * @param {string} keyword 
         */
        async getSearch(context, keyword) {
            context.commit('SET_ARTICLES', [])
            const res = await api.getSearch(keyword)
            context.commit('SET_ARTICLES', res.data.data.results)
        },
        /**
         * @param {string} keyword 
         */
        async suggestion(context, keyword) {
            context.commit('SET_ARTICLES', [])
            const res = await api.suggestion(keyword)
            context.commit('SET_ARTICLES', res.data.data.results)
        },
        async getNews(context, article_id) {
            context.commit('SET_NEWS', [])
            const res = await api.getUserNews(article_id)
            context.commit('SET_NEWS', res.data.data)
        },
    },
}